package com.yangxk.kadmin.common.util;

import java.text.MessageFormat;

/**
 * 字符串工具类
 *
 * @author yangxk
 * @date 2018/9/12 15:27
 */
public class StringUtil {
    private StringUtil(){}

    /**
     * 数组转为字符串
     * @param arr  需要转换的数组
     * @param regex 分隔符
     * create by yangxk on 2018/9/12 15:26
     */
    public static String arr2String(Object[] arr, String regex) {
        if (arr == null || arr.length == 0 || regex == null)
            return null;
        StringBuilder sb = new StringBuilder();
        for (Object o : arr) {
            sb.append(o);
            sb.append(regex);
        }

        String res = sb.toString();

        return res.substring(0, res.length() - 1);
    }

    /**
     * 数组转换为字符串并用逗号(,)分割
     * create by yangxk on 2018/9/12 15:28
     */
    public static String arr2String(Object[] arr) {
        return arr2String(arr, ",");
    }

    /**
     * 将字符串转换为数组
     * @param value 需要转换的字符串
     * @param regex 分隔符
     * create by yangxk on 2018/9/12 15:30
     */
    public static String[] str2Arr(String value, String regex) {
        if (value == null || "".equals(value) || regex == null) {
            return null;
        }

        String[] arr = value.split(regex);

        return arr;
    }

    /**
     * 将字符串转换为数组并用逗号(,)分割
     * create by yangxk on 2018/9/12 15:31
     */
    public static String[] str2Arr(String value) {
        return str2Arr(value, ",");
    }

    /**
     * 首字母大写
     * create by yangxk on 2018/9/12 15:47
     */
    public static String firstUpper(String str) {
        return org.apache.commons.lang3.StringUtils.capitalize(str);
    }

    /**
     * 首字母小写
     * create by yangxk on 2018/9/12 15:47
     */
    public static String firstLower(String str) {
        return org.apache.commons.lang3.StringUtils.uncapitalize(str);
    }

    public static String format(String str, Object...objects) {
        if (str == null || "".equals(str)) {
            return "";
        }

        String format = MessageFormat.format(str, objects);
        return format;
    }

    public static void main(String[] args) {
        String str = "/{0}/list";
        System.out.println(format(str, "user"));
    }
}
